using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using E4RPTools;
using Movements.DataSet;

namespace Movements.Controls
{
    public partial class SupplierXtraForm : XtraForm
    {
        private long _documentId;
        private Controller _controller;
        private FornecedorDataSet _dataSet;

        #region Properties

        public long DocumentId
        {
            set { _documentId = value; }
        }

        public Controller Controller
        {
            set { _controller = value; }
        }

        #endregion

        public SupplierXtraForm()
        {
            InitializeComponent();
            Load += SupplierXtraFormLoad;
            OKSimpleButton.Click += OKSimpleButtonClick;
        }

        #region Private Methods

        private void MergeData()
        {
            _dataSet = new FornecedorDataSet();
            if (_documentId <= 0)
            {
                var supplier = _dataSet.Fornecedor.NewFornecedorRow();
                supplier.Id = _documentId;
                supplier.Nome = "";
                supplier.Contribuinte = "";
                supplier.Morada = "";
                supplier.CodigoPostal = "";
                supplier.Telefone = "";
                supplier.Telemovel = "";
                supplier.Email = "";
                supplier.Contacto = "";
                _dataSet.Fornecedor.AddFornecedorRow(supplier);
            }
            else
                _dataSet.Merge(_controller.GetSupplier(_documentId));
        }

        private void SetDataToControls()
        {
            nameTextEdit.Text = _dataSet.Fornecedor[0].Nome;
            fiscalNumberTextEdit.Text = _dataSet.Fornecedor[0].IsContribuinteNull() ? "" : _dataSet.Fornecedor[0].Contribuinte;
            addressTextEdit.Text = _dataSet.Fornecedor[0].IsMoradaNull() ? "" : _dataSet.Fornecedor[0].Morada;
            zipCodeTextEdit.Text = _dataSet.Fornecedor[0].IsCodigoPostalNull() ? "" : _dataSet.Fornecedor[0].CodigoPostal;
            phoneTextEdit.Text = _dataSet.Fornecedor[0].IsTelefoneNull() ? "" : _dataSet.Fornecedor[0].Telefone;
            mobileTextEdit.Text = _dataSet.Fornecedor[0].IsTelemovelNull() ? "" : _dataSet.Fornecedor[0].Telemovel;
            mailTextEdit.Text = _dataSet.Fornecedor[0].IsEmailNull() ? "" : _dataSet.Fornecedor[0].Email;
            contactTextEdit.Text = _dataSet.Fornecedor[0].IsContactoNull() ? "" : _dataSet.Fornecedor[0].Contacto;
        }

        private void GetDataFromControls()
        {
            _dataSet.Fornecedor[0].Nome = nameTextEdit.Text;
            _dataSet.Fornecedor[0].Contribuinte = fiscalNumberTextEdit.Text;
            _dataSet.Fornecedor[0].Morada = addressTextEdit.Text;
            _dataSet.Fornecedor[0].CodigoPostal = zipCodeTextEdit.Text;
            _dataSet.Fornecedor[0].Telefone = phoneTextEdit.Text;
            _dataSet.Fornecedor[0].Telemovel = mobileTextEdit.Text;
            _dataSet.Fornecedor[0].Email = mailTextEdit.Text;
            _dataSet.Fornecedor[0].Contacto = contactTextEdit.Text;
        }

        private void ClearControlErrors()
        {
            dxErrorProvider.SetError(nameTextEdit, "");
            dxErrorProvider.SetError(fiscalNumberTextEdit, "");
            dxErrorProvider.SetError(addressTextEdit, "");
            dxErrorProvider.SetError(zipCodeTextEdit, "");
            dxErrorProvider.SetError(phoneTextEdit, "");
            dxErrorProvider.SetError(mobileTextEdit, "");
            dxErrorProvider.SetError(mailTextEdit, "");
            dxErrorProvider.SetError(contactTextEdit, "");
        }

        private Control GetControl(string reference)
        {
            switch (reference)
            {
                case "Nome":
                    return nameTextEdit;
                case "Contribuinte":
                    return fiscalNumberTextEdit;
                case "Morada":
                    return addressTextEdit;
                case "CodigoPostal":
                    return zipCodeTextEdit;
                case "Telefone":
                    return phoneTextEdit;
                case "Telemovel":
                    return mobileTextEdit;
                case "Email":
                    return mailTextEdit;
                case "Contacto":
                    return contactTextEdit;
                default:
                    return null;
            }
        }

        private void SetControlErrors(Result result)
        {
            foreach (var error in result.Errors)
                dxErrorProvider.SetError(GetControl(error.Reference), error.Message);
        }

        #endregion

        void SupplierXtraFormLoad(object sender, EventArgs e)
        {
            MergeData();
            SetDataToControls();
        }

        void OKSimpleButtonClick(object sender, EventArgs e)
        {
            ClearControlErrors();
            GetDataFromControls();
            var result = _controller.IsSupplierValid(_dataSet);
            if (result.Failed)
            {
                DialogResult = DialogResult.None;
                SetControlErrors(result);
            }
            else
                _controller.SaveSupplier(_dataSet);
        }
    }
}